package com.cash.app.ui.auth

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.viewModels
import com.cash.app.R
import com.cash.app.base.BaseActivity
import com.cash.app.custom.AppActionBar
import com.cash.app.databinding.AcAuthBankBinding
import com.cash.app.ui.auth.model.AuthBankInfoResponse
import com.cash.app.ui.auth.model.BindBankRequest
import com.cash.app.utils.hide

class AuthBankActivity : BaseActivity() {

    private var isOnlyShow = false

    companion object {
        private const val EXTRA_ONLY_SHOW = "isOnlyShow"

        fun start(context: Context, isOnlyShow: Boolean = false) {
            val intent = Intent(context, AuthBankActivity::class.java).apply {
                putExtra(EXTRA_ONLY_SHOW, isOnlyShow)
            }
            context.startActivity(intent)
        }
    }

    private lateinit var binding: AcAuthBankBinding
    private val viewModel: AuthBankViewModel by viewModels()

    override fun setViewBind() {
        binding = AcAuthBankBinding.inflate(layoutInflater)
    }

    override fun getLayoutView(): View = binding.root

    override fun getAppActionBar(): AppActionBar = binding.actionBar

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
        observeData()
        isOnlyShow = intent.getBooleanExtra(EXTRA_ONLY_SHOW, false)
        if (isOnlyShow) {
            viewModel.getAuthInfoByBank()
        }
    }

    private fun initView() {
        binding.btnSubmit.setOnClickListener {
            val ifscCode = binding.etIFSC.text.toString().trim()
            val bankAccount = binding.etBankAccount.text.toString().trim()
            val confirmAccount = binding.etConfirmAccount.text.toString().trim()

            if (ifscCode.isEmpty() || bankAccount.isEmpty() || confirmAccount.isEmpty()) {
                showToast(R.string.auth_error_enter_ifsc_code)
                return@setOnClickListener
            }
            if (bankAccount.isEmpty() || confirmAccount.isEmpty()) {
                showToast(R.string.auth_error_enter_bank_account)
                return@setOnClickListener
            }

            if (bankAccount != confirmAccount) {
                showToast(R.string.auth_error_bank_account_same)
                return@setOnClickListener
            }

            viewModel.submitBindBank(
                BindBankRequest(
                    ifscCode = ifscCode, ifscNumber = bankAccount
                )
            )
        }
    }

    private fun showToast(strResId: Int) {
        Toast.makeText(this, getString(strResId), Toast.LENGTH_LONG).show()
    }

    private fun observeData() {
        viewModel.bindBankInfo.observe(this) { response ->
            //showToast(R.string.auth_success)
            setResult(Activity.RESULT_OK)
            finish()
        }
        viewModel.authBankInfo.observe(this) { bankInfo: AuthBankInfoResponse ->
            binding.apply {
                btnSubmit.hide()
                etIFSC.isEnabled = false
                etBankAccount.isEnabled = false
                etConfirmAccount.isEnabled = false
                etIFSC.setText(bankInfo.bankBankCode)
                etBankAccount.setText(bankInfo.bankCardNumber)
                etConfirmAccount.setText(bankInfo.bankCardNumber)
            }
        }
    }
} 